package com.tbs.easyhomemoneyserver.dao

import com.tbs.easyhomemoneyserver.DO.HomeGroup
import com.tbs.easyhomemoneyserver.dao.providers.HomeProvider
import com.tbs.easyhomemoneyserver.form.HomeSearchForm
import com.tbs.easyhomemoneyserver.model.dto.HomeMemberChangePreCheckDTO
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.SelectProvider
import tk.mybatis.mapper.common.BaseMapper

@Mapper
interface HomeDao : BaseMapper<HomeGroup> {
    @Select(
        "select h.*," +
                "IF(hm.member_type=0,1,0) as parent_flag" +
                " from home h " +
                "join  home_member hm on " +
                "h.id=hm.home_id " +
                "and hm.child_id=#{param1} " +
                "and h.delete_flag=0 " +
                "and hm.enable=1  " +
                "group by hm.home_id,parent_flag"
    )
    fun findByUserId(userid: Long): List<HomeGroup>


    @SelectProvider(type = HomeProvider::class, method = "findByForm")
    fun findHomeBySearchForm(homeSearchForm: HomeSearchForm): List<HomeGroup>;

    @Select(
        "select COUNT(h.id) as hasGroup ," +
                "COUNT(hmp.id) as isParent," +
                "COUNT(ps.id) as hasPerson " +
                "from home h " +
                "left join home_member hmp on " +
                "h.id = hmp.home_id " +
                "and hmp.enable=1 " +
                "and hmp.child_id=#{changeUserId}" +
                " left join sys_user ps on" +
                " ps.id=#{personId}" +
                "  where h.id=#{homeId}"
    )
    fun preCheckForMemberChange(personId: Long, changeUserId: Long, homeId: Long): HomeMemberChangePreCheckDTO?
}
